package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.RechargeTermModel;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.util.Arith;
import com.yiwei.ydd.util.Dialog;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class PacketTypeAdapter extends ItemAdapter<RechargeTermModel.DatasBean.GitBean, PacketTypeAdapter.DefaultHolder> {


    private Context context;

    public PacketTypeAdapter(Context context) {
        super(context);
        this.context = context;
    }

    private double one_money = 0;
    private String select_git;
    private String select_periods;

    public String getSelect_git() {
        return select_git;
    }

    public String getSelect_periods() {
        return select_periods;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_packet_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        RechargeTermModel.DatasBean.GitBean item = getItem(position);


        String act_money;
        String total_money;
        String git_money;


        if (one_money != 0) {


            act_money = Arith.mul(Double.valueOf(item.periods), one_money) + "";
            git_money = Arith.mul(Double.valueOf(act_money), Double.valueOf(item.git) * 0.01) + "";
            total_money = Arith.add(Double.valueOf(act_money), Double.valueOf(git_money)) + "";
            holder.txtTitle.setText(item.name + "(即多送" + git_money + "元)");

        } else {
            holder.txtTitle.setText(item.name);
            act_money = "";
            total_money = "";
            git_money = "";
        }

        if (Float.valueOf(item.inventory) <= 0) {
            holder.layoutTotal.setBackgroundResource(R.drawable.bg_packet_type_unselect);
//            holder.layoutTotal.setBackgroundResource(R.drawable.bg_money_unselect);
//            holder.txtMoney.setTextColor(0xffCCCCCC);
            holder.txtDesc.setTextColor(0xffCCCCCC);
            holder.txtTitle.setTextColor(0xffCCCCCC);
        } else {

            if (item.isCheck) {
                select_git = item.git;
                select_periods = item.periods;
                holder.layoutTotal.setBackgroundResource(R.drawable.bg_packet_type_select);
                holder.btnCheck.setImageResource(R.mipmap.img_packet_check);
//                if (item.recommend.equals("0")) {
//                    holder.layoutTotal.setBackgroundResource(R.mipmap.img_money_select);
//                    holder.txtMoney.setTextColor(0xffFB5E01);
//                    holder.txtDesc.setTextColor(0xffFB5E01);
//                    holder.txtTitle.setTextColor(0xffFB5E01);
//                } else {
//                    holder.layoutTotal.setBackgroundResource(R.mipmap.img_oil_sign_bg_check_sec);
//                    holder.txtMoney.setTextColor(0xffffffff);
//                    holder.txtDesc.setTextColor(0xffffffff);
//                    holder.txtTitle.setTextColor(0xffffffff);
//                }
//            } else {
//                if (item.recommend.equals("0")) {
//                    holder.layoutTotal.setBackgroundResource(R.drawable.bg_money_unselect);
//                    holder.txtMoney.setTextColor(0xffFF0030);
//                    holder.txtDesc.setTextColor(0xff666666);
//                    holder.txtTitle.setTextColor(0xff000000);
//                } else {
//                    holder.layoutTotal.setBackgroundResource(R.mipmap.img_oil_sign_bg_sec);
//                    holder.txtMoney.setTextColor(0xffffffff);
//                    holder.txtDesc.setTextColor(0xffffffff);
//                    holder.txtTitle.setTextColor(0xffffffff);
//                }
            }else {
                holder.layoutTotal.setBackgroundResource(R.drawable.bg_packet_type_unselect);
                holder.btnCheck.setImageResource(R.mipmap.img_packet_uncheck);
            }
        }
        if(item.recommend.equals("1")){
            holder.imgSign.setVisibility(View.VISIBLE);
            holder.imgSign.setImageResource(R.mipmap.img_packet_sign_default);
        }else {
            if(item.level.equals("2")){
                holder.imgSign.setVisibility(View.VISIBLE);
                holder.imgSign.setImageResource(R.mipmap.img_packet_sign_jin);
            }else if(item.level.equals("3")){
                holder.imgSign.setVisibility(View.VISIBLE);
                holder.imgSign.setImageResource(R.mipmap.img_packet_sign_bai);
            }else {
                holder.imgSign.setVisibility(View.GONE);
            }
        }



        holder.txtDesc.setText(item.desc);
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Float.valueOf(item.inventory) <= 0) {
                    Dialog.showDefaultOneBtnDialog(context,  "该赠送套餐已售馨，\n请明天再来购买吧", "好的，我知道了", new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {

                        }

                        @Override
                        public void cancel() {

                        }
                    });
                    return;
                }
                if (Float.valueOf(item.level) > Float.valueOf(UserInfoGlobal.getUserLevel())) {
                    if (onChooseListener != null) {
                        onChooseListener.showLevelDialog(item.level, item.git);
                    }
                    return;
                }
                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.id, total_money, git_money, act_money, item.periods, item.git);
                }
            }
        });
    }

    public void setOneMoney(double one_money) {
        this.one_money = one_money;
        notifyDataSetChanged();
    }

    class DefaultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_desc)
        TextView txtDesc;
        @BindView(R.id.img_sign)
        ImageView imgSign;
        @BindView(R.id.btn_check)
        ImageView btnCheck;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;


        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id, String total_money, String git_money, String act_money, String period, String git);

        void showLevelDialog(String level, String git);
    }
}
